package com.mloeppky.setgame.client;

import static com.google.common.collect.Lists.newArrayList;

import java.util.Collection;
import java.util.List;

/**
 * {@link SetGame} utility methods.
 * @author mloeppky
 *
 */
public class SetUtils {
	
	public static final String BACKGROUND_STYLE = "gradient4";
	
	/**
	 * We don't want this class to be instantiated
	 */
	private SetUtils() {}
	
	/**
	 * Determines if the given cards are a set or not. Cards are a set if their
	 * qty, fill, color, and shape are either all equal or all different.
	 * @param cards The cards to check if are a set. Cards should only contain
	 * 3 elements.
	 * @return True if they are set. False otherwise.
	 * @see <a href="http://www.setgame.com/set/rules_set.htm">Rules</a>
	 */
	public static boolean isSet(Collection<SetCard> cards) {
		//A set can only contain 3 cards
		if (cards.size() != 3) {
			throw new IllegalArgumentException(cards + " should only contain 3 elements");
		} else {
			//Convert the cards to a list so we can get each element
			List<SetCard> cardList = newArrayList(cards);
			SetCard sc1 = cardList.get(0);
			SetCard sc2 = cardList.get(1);
			SetCard sc3 = cardList.get(2);
			
			//Determine if the three cards are a set by checking that each trait
			//is either equal or different for all three cards.
			return allEqualOrDifferent(sc1.getQuantity(), sc2.getQuantity(), sc3.getQuantity())
				&& allEqualOrDifferent(sc1.getFill(), sc2.getFill(), sc3.getFill())
				&& allEqualOrDifferent(sc1.getColor(), sc2.getColor(), sc3.getColor())
				&& allEqualOrDifferent(sc1.getShape(), sc2.getShape(), sc3.getShape());
		}
	}
	
	/**
	 * Determines if all three objects are all equal or all not equal.
	 * @param o1
	 * @param o2
	 * @param o3
	 * returns True if all three objects are all equal. True if all three
	 * objects are not equal. False otherwise.
	 */
	private static boolean allEqualOrDifferent(Object o1, Object o2, Object o3) {
		return (o1.equals(o2) && o2.equals(o3) && o1.equals(o3)) || 
				(!o1.equals(o2) && !o2.equals(o3) && !o1.equals(o3));
	}
}
